<?php
namespace App\Http\Strategy;

use App\Common\ErrorCode\ErrorCode;
use App\Exceptions\BusinessException;
use App\Http\Service\JwtService;
use App\Models\User\UserModel;
use http\Exception\BadUrlException;
use App\Models\Admin\UserAccessModel;

class UserStrategy
{
    private $user;

    public function __construct(UserModel $user)
    {
        $this->user = $user;
    }

    /**
     * 用户登陆方法
     * @param $phone
     * @param $pwd
     * @return array
     * @throws BusinessException
     */
    public function login($phone, $pwd) {
        $user = $this->user->newQuery()->where(['phone' => $phone])->first();
        if (empty($user) || !password_verify($pwd, $user->getAttribute('password'))) {
            throw new BusinessException(ErrorCode::ERROR_LOGIN_ACCOUNT_OR_PASSWORD);
        }
        //zwp,修改token获取，原来的作者没写完，
        session(["loginUser"=>$user]);
        session()->save();
        $retData = [
            'type' => $user->getAttribute('type'),
            "uid"  =>$user->id
        ];
        return $retData;
    }
    //备份login方法，
    public function loginBackUp($phone, $pwd) {
        $user = $this->user->newQuery()->where(['phone' => $phone])->first();
        if (empty($user) || !password_verify($pwd, $user->getAttribute('password'))) {
            throw new BusinessException(ErrorCode::ERROR_LOGIN_ACCOUNT_OR_PASSWORD);
        }
        $token = JwtService::encode(['uid' => $user->getAttribute('id'), 'type' => $user->getAttribute('type')]);
        //zwp,修改token获取，原来的作者没写完，
        $retData = [
            'type' => $user->getAttribute('type'),
            'token' => (string)$token
        ];
        return $retData;
    }
    
    /**
     * 用户重置密码，暂时没有考虑验证码的问题
     * @param $phone
     * @param $newPwd
     * @param $code
     */
    public function resetPwd(int $phone, string $newPwd, int $code,$resYzm)
    {
        if(intval($code)!=intval($resYzm)){
            return false;
        }
        $user = $this->user->newQuery()->where(['phone' => $phone])->first();
        $user->setAttribute('password', password_hash($newPwd, PASSWORD_DEFAULT));
        $user->save();
        return true;
    }

    /**
     * 注册添加用户，不填写任何注册信息
     * @param $phone
     * @param $pwd
     * @param $code
     * @throws BusinessException
     */
    public function addUser($phone, $pwd, $code)
    {
        $user = $this->user->newQuery()->where(['phone'=> $phone])->first();
        if (!empty($user)) {
            throw new BusinessException(ErrorCode::ERROR_PARAM, '手机号已注册');
        }
        //添加新的用户信息
        $this->user->phone = $phone;
        $this->user->name = "用户".$phone;
        $this->user->password = password_hash($pwd,PASSWORD_DEFAULT);
        $this->user->save();
        
    }

    //zwp，返回注册用户的id，判断注册页面是否已经注册过，
    public function getUid($phone){
        $user = $this->user->newQuery()->where(['phone'=> $phone])->first();
        if (!empty($user)) {
            return $this->user->id;
        }else{
            return null;
        }
    }

        /**
     * 设置用户主体类型
     * @param $userId
     * @param $type
     * @param $introducer
     * @throws BusinessException
     */
    public function setUserType($userId, $type, $introducer = 0)
    {
        //var_dump($userId);exit();
        if($userId==null){
            throw new BusinessException(ErrorCode::ERROR_USER_NO_PERMISSION, '请先注册');
        }
        $user = $this->user->newQuery()->where(['id' => $userId])->first();
        if ($user->getAttribute('type') != 0) {
            throw new BusinessException(ErrorCode::ERROR_USER_NO_PERMISSION, '用户已设置主体类型，不可更改');
        }

        $user->setAttribute('type', $type);
        $user->setAttribute('updated_at', time());

        if (!empty($introducer)){
            $user->setAttribute('introducer', $introducer);
        }
        if($user->save()){
            return true;//zwp,增加返回值
        }else{
            return false;//zwp,
        }
        
    }

    /**
     * 验证码登陆（还没有验证验证码操作）
     * @param $phone
     * @param $code
     * @throws BusinessException
     * @return array
     */
    public function codeLogin($phone, $code)
    {
        $user = $this->user->newQuery()->where(['phone' => $phone])->first();
        if (empty($user)) {
            throw new BusinessException(ErrorCode::ERROR_USER_UNDEFINED, '用户不存在');
        }

        //$token = JwtService::encode(['uid' => $user->getAttribute('id'), 'type' => $user->getAttribute('type')]);
        //修改token获取，zwp
        session(["loginUser"=>$user]);
        session()->save();
        $retData = [
            'type' => $user->getAttribute('type'),
            'uid' => $user->id
        ];
        return $retData;
    }
    
}